{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Abbreviating the Product of a Range"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Hard"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #math"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数学"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: abbreviateProduct"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #一个区间内所有数乘积的缩写"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你两个正整数&nbsp;<code>left</code>&nbsp;和&nbsp;<code>right</code>&nbsp;，满足&nbsp;<code>left &lt;= right</code>&nbsp;。请你计算&nbsp;<strong>闭区间</strong>&nbsp;<code>[left, right]</code>&nbsp;中所有整数的&nbsp;<strong>乘积</strong>&nbsp;。</p>\n",
    "\n",
    "<p>由于乘积可能非常大，你需要将它按照以下步骤 <strong>缩写</strong>&nbsp;：</p>\n",
    "\n",
    "<ol>\n",
    "\t<li>统计乘积中&nbsp;<strong>后缀</strong> 0 的数目，并 <strong>移除</strong> 这些 0 ，将这个数目记为&nbsp;<code>C</code>&nbsp;。\n",
    "\n",
    "\t<ul>\n",
    "\t\t<li>比方说，<code>1000</code>&nbsp;中有 <code>3</code> 个后缀 0&nbsp;，<code>546</code>&nbsp;中没有后缀 0 。</li>\n",
    "\t</ul>\n",
    "\t</li>\n",
    "\t<li>将乘积中剩余数字的位数记为&nbsp;<code>d</code>&nbsp;。如果&nbsp;<code>d &gt; 10</code>&nbsp;，那么将乘积表示为&nbsp;<code>&lt;pre&gt;...&lt;suf&gt;</code>&nbsp;的形式，其中&nbsp;<code>&lt;pre&gt;</code>&nbsp;表示乘积最 <strong>开始</strong>&nbsp;的 <code>5</code>&nbsp;个数位，<code>&lt;suf&gt;</code>&nbsp;表示删除后缀 0 <strong>之后</strong>&nbsp;结尾的 <code>5</code>&nbsp;个数位。如果&nbsp;<code>d &lt;= 10</code>&nbsp;，我们不对它做修改。\n",
    "\t<ul>\n",
    "\t\t<li>比方说，我们将&nbsp;<code>1234567654321</code>&nbsp;表示为&nbsp;<code>12345...54321</code>&nbsp;，但是&nbsp;<code>1234567</code>&nbsp;仍然表示为&nbsp;<code>1234567</code>&nbsp;。</li>\n",
    "\t</ul>\n",
    "\t</li>\n",
    "\t<li>最后，将乘积表示为 <strong>字符串</strong>&nbsp;<code>\"&lt;pre&gt;...&lt;suf&gt;eC\"</code>&nbsp;。\n",
    "\t<ul>\n",
    "\t\t<li>比方说，<code>12345678987600000</code>&nbsp;被表示为&nbsp;<code>\"12345...89876e5\"</code>&nbsp;。</li>\n",
    "\t</ul>\n",
    "\t</li>\n",
    "</ol>\n",
    "\n",
    "<p>请你返回一个字符串，表示 <strong>闭区间</strong>&nbsp;<code>[left, right]</code>&nbsp;中所有整数&nbsp;<strong>乘积</strong>&nbsp;的&nbsp;<strong>缩写</strong>&nbsp;。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<b>输入：</b>left = 1, right = 4\n",
    "<b>输出：</b>\"24e0\"\n",
    "<strong>解释：</strong>\n",
    "乘积为 1 × 2 × 3 × 4 = 24 。\n",
    "由于没有后缀 0 ，所以 24 保持不变，缩写的结尾为 \"e0\" 。\n",
    "因为乘积的结果是 2 位数，小于 10 ，所欲我们不进一步将它缩写。\n",
    "所以，最终将乘积表示为 \"24e0\" 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>left = 2, right = 11\n",
    "<strong>输出：</strong>\"399168e2\"\n",
    "<strong>解释：</strong>乘积为 39916800 。\n",
    "有 2 个后缀 0 ，删除后得到 399168 。缩写的结尾为 \"e2\" 。 \n",
    "删除后缀 0 后是 6 位数，不需要进一步缩写。 \n",
    "所以，最终将乘积表示为 \"399168e2\" 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>left = 371, right = 375\n",
    "<strong>输出：</strong>\"7219856259e3\"\n",
    "<strong>解释：</strong>乘积为 7219856259000 。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= left &lt;= right &lt;= 10<sup>4</sup></code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [abbreviating-the-product-of-a-range](https://leetcode.cn/problems/abbreviating-the-product-of-a-range/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [abbreviating-the-product-of-a-range](https://leetcode.cn/problems/abbreviating-the-product-of-a-range/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['1\\n4', '2\\n11', '371\\n375']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def abbreviateProduct(self, left: int, right: int) -> str:\n",
    "        C, maxpre, mod, maxval = 0, 10 ** 24, 10 ** 10, 10 ** 12\n",
    "        val, pre, suf = 1,1,1\n",
    "        for i in range(left, right + 1):\n",
    "            pre *= i\n",
    "            suf *= i\n",
    "            last = 0\n",
    "            while pre > maxpre :\n",
    "                last = pre % 10\n",
    "                pre = pre // 10\n",
    "            if last >= 5:\n",
    "                pre += 1\n",
    "            while suf % 10 == 0:\n",
    "                suf //= 10\n",
    "                C += 1\n",
    "            suf %= mod\n",
    "            if val <= maxval:\n",
    "                val *= i\n",
    "                while val % 10 == 0:\n",
    "                    val //= 10\n",
    "        \n",
    "        if len(str(val)) <= 10:\n",
    "            return str(val) + 'e' + str(C)\n",
    "        else:\n",
    "            p, s = str(pre), str(suf)\n",
    "            while len(p) > 5:\n",
    "                p = p[:-1]\n",
    "            while len(s) > 5:\n",
    "                s = s[1:]\n",
    "            while len(s) < 5:\n",
    "                s = '0' + s\n",
    "            return p + '...' + s + 'e' + str(C)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "T1, T2 = int(10 ** 20), int(10 ** 15)\n",
    "class Solution:\n",
    "    def abbreviateProduct(self, left: int, right: int) -> str:\n",
    "        print(T1)\n",
    "        num1, num2 = left, left\n",
    "        flag = False\n",
    "        for i in range(left + 1, right + 1):\n",
    "            num1, num2 = num1 * i, num2 * i\n",
    "            if num1 > T1 and num1 % 10 != 0:\n",
    "                flag = True\n",
    "            while num1 > T1:\n",
    "                num1 //= 10\n",
    "            while num2 % 10 == 0:\n",
    "                num2 //= 10\n",
    "            num2 = int(num2) % T2\n",
    "        zero = \"e\" + str(self.count_zero(left, right))\n",
    "        if not flag:\n",
    "            while num1 % 10 == 0:\n",
    "                num1 //= 10\n",
    "        if num1 < 10 ** 10:\n",
    "            return str(num1) + zero\n",
    "        while num1 >= 10 ** 5:\n",
    "            num1 //= 10\n",
    "        temp = str(num2)\n",
    "        return str(num1) + \"...\" + temp[len(temp) - 5:] + zero\n",
    "\n",
    "\n",
    "    def count_zero(self, left, right):\n",
    "        two, five = 0, 0\n",
    "\n",
    "        def f(num):\n",
    "            a, b = 0, 0\n",
    "            while num % 2 == 0:\n",
    "                num, a = num / 2, a + 1\n",
    "            while num % 5 == 0:\n",
    "                num, b = num / 5, b + 1\n",
    "            return a, b\n",
    "\n",
    "        for i in range(left, right + 1):\n",
    "            temp = f(i)\n",
    "            two, five = two + temp[0], five + temp[1]\n",
    "        return min(two, five)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def abbreviateProduct(self, left, right):\n",
    "        \"\"\"\n",
    "        :type left: int\n",
    "        :type right: int\n",
    "        :rtype: str\n",
    "        \"\"\"\n",
    "        import sys\n",
    "        p=1\n",
    "        for i in range(left,right+1):\n",
    "           p=p*i\n",
    "        sys.set_int_max_str_digits(0)\n",
    "        q=str(p)\n",
    "        l=0\n",
    "        for k in q[::-1]:\n",
    "           if k!='0':\n",
    "              break\n",
    "           else:\n",
    "              l=l+1\n",
    "        if l==0:\n",
    "           return f\"{q}e0\" \n",
    "        t=q[:-l]\n",
    "        if len(t)>10:\n",
    "            return f'{t[:5]}...{t[-5:]}e{str(l)}'\n",
    "        return  f'{t}e{str(l)}'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def abbreviateProduct(self, left: int, right: int) -> str:\n",
    "        sys.set_int_max_str_digits(10**9)\n",
    "        product = 1\n",
    "        for a in range(left, right+1):\n",
    "            product *= a\n",
    "        product = str(product)\n",
    "        n = len(product)\n",
    "        i = n-1\n",
    "        while product[i] == '0':\n",
    "            i-=1\n",
    "        trailzero = n-i-1\n",
    "        ans = 'e'+ str(trailzero)\n",
    "        leading = n - trailzero\n",
    "        if leading <= 10:\n",
    "            strleading = product[:leading]\n",
    "        else:\n",
    "            strleading = product[:5] + '...' + product[leading-5:leading]\n",
    "        return strleading+ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def abbreviateProduct(self, left: int, right: int) -> str:\n",
    "        M = 10 ** 10\n",
    "        MOD = 10 ** 20    # 以前 14 行，现在改成 16    \n",
    "        zero = 0   \n",
    "        # 计算后缀，并统计末位 0 的个数\n",
    "        suf = 1\n",
    "        for i in range(left, right + 1):\n",
    "            suf *= i\n",
    "            while suf % 10 == 0:\n",
    "                zero += 1\n",
    "                suf //= 10\n",
    "            \n",
    "            suf %= MOD\n",
    "\n",
    "        if suf < M: return str(suf) + 'e' + str(zero)   \n",
    "        \n",
    "        # 计算前缀\n",
    "        pref = 1\n",
    "        for i in range(left, right + 1):\n",
    "            pref *= i\n",
    "            while pref >= MOD:\n",
    "                pref //= 10 \n",
    "\n",
    "        return str(pref)[:5] + '...' + str(suf)[-5:] + 'e' + str(zero)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "sys.set_int_max_str_digits(10**9)\n",
    "class Solution:\n",
    "    def abbreviateProduct(self, left: int, right: int) -> str:\n",
    "        ans = 1\n",
    "        for i in range(left, right + 1):\n",
    "            ans *= i\n",
    "        ans = str(ans)\n",
    "        count = 0\n",
    "        for i in range(1, len(ans)):\n",
    "            if(ans[-i] == '0'):\n",
    "                count += 1\n",
    "            else:\n",
    "                break\n",
    "        ans = ans[:len(ans) - count]\n",
    "        if(len(ans) <= 10):\n",
    "            return ans + \"e\" + str(count)\n",
    "        else:\n",
    "            return ans[:5] + \"...\" + ans[-5:] + \"e\" + str(count)\n",
    "            \n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def abbreviateProduct(self, left, right):\n",
    "        \"\"\"\n",
    "        :type left: int\n",
    "        :type right: int\n",
    "        :rtype: str\n",
    "        \"\"\"\n",
    "        import sys\n",
    "        p=1\n",
    "        for i in range(left,right+1):\n",
    "           p=p*i\n",
    "        sys.set_int_max_str_digits(0)\n",
    "        q=str(p)\n",
    "        l=0\n",
    "        for k in q[::-1]:\n",
    "           if k!='0':\n",
    "              break\n",
    "           else:\n",
    "              l=l+1\n",
    "        if l==0:\n",
    "           return f\"{q}e0\" \n",
    "        t=q[:-l]\n",
    "        if len(t)>10:\n",
    "            return f'{t[:5]}...{t[-5:]}e{str(l)}'\n",
    "        return  f'{t}e{str(l)}'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def abbreviateProduct(self, left: int, right: int) -> str:\n",
    "        C, maxpre, mod, maxval = 0, 10 ** 24, 10 ** 10, 10 ** 12\n",
    "        val, pre, suf = 1,1,1\n",
    "        for i in range(left, right + 1):\n",
    "            pre *= i\n",
    "            suf *= i\n",
    "            last = 0\n",
    "            while pre > maxpre :\n",
    "                last = pre % 10\n",
    "                pre = pre // 10\n",
    "            if last >= 5:\n",
    "                pre += 1\n",
    "            while suf % 10 == 0:\n",
    "                suf //= 10\n",
    "                C += 1\n",
    "            suf %= mod\n",
    "            if val <= maxval:\n",
    "                val *= i\n",
    "                while val % 10 == 0:\n",
    "                    val //= 10\n",
    "        \n",
    "        if len(str(val)) <= 10:\n",
    "            return str(val) + 'e' + str(C)\n",
    "        else:\n",
    "            p, s = str(pre), str(suf)\n",
    "            while len(p) > 5:\n",
    "                p = p[:-1]\n",
    "            while len(s) > 5:\n",
    "                s = s[1:]\n",
    "            while len(s) < 5:\n",
    "                s = '0' + s\n",
    "            return p + '...' + s + 'e' + str(C)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def abbreviateProduct(self, left: int, right: int) -> str:\n",
    "\n",
    "        sys.set_int_max_str_digits(0)\n",
    "        ans = 1\n",
    "        c = 0\n",
    "        for i in range(left,right+1):\n",
    "            ans *= i\n",
    "        ans = str(ans)\n",
    "        while ans[-1] == '0':\n",
    "            c += 1\n",
    "            ans = ans[:-1]\n",
    "        return ans + 'e' + str(c) if len(ans) <=10 else ans[:5] +'...' + ans[-5:] +'e' +str(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def abbreviateProduct(self, left: int, right: int) -> str:\n",
    "        import sys\n",
    "        sys.set_int_max_str_digits(0)\n",
    "        ans = 1\n",
    "        c = 0\n",
    "        for i in range(left,right+1):\n",
    "            ans *= i\n",
    "        ans = str(ans)\n",
    "        while ans[-1] == '0':\n",
    "            c += 1\n",
    "            ans = ans[:-1]\n",
    "        return ans + 'e' + str(c) if len(ans) <=10 else ans[:5] +'...' + ans[-5:] +'e' +str(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def abbreviateProduct(self, left: int, right: int) -> str:\n",
    "        first = last = 1\n",
    "        count_two = count_five = 0\n",
    "        flag = False\n",
    "        for num in range(left, right + 1):\n",
    "            while num % 2 == 0:\n",
    "                num //= 2\n",
    "                count_two += 1\n",
    "            while num % 5 == 0:\n",
    "                num //= 5\n",
    "                count_five += 1\n",
    "            last *= num\n",
    "            if len(str(last)) > 30:\n",
    "                first = int(str(last)[:25])\n",
    "                flag = True\n",
    "            elif first > 1:\n",
    "                first = int(str(first * num)[:25])\n",
    "            if flag:\n",
    "                last = int(str(last)[-5:])\n",
    "        count_zero = min(count_two, count_five)\n",
    "        for _ in range(count_two - count_zero):\n",
    "            last *= 2\n",
    "            if len(str(last)) > 30:\n",
    "                first = int(str(last)[:25])\n",
    "                flag = True\n",
    "            elif first > 1:\n",
    "                first = int(str(first * 2)[:25])\n",
    "            if flag:\n",
    "                last = int(str(last)[-5:])\n",
    "        for _ in range(count_five - count_zero):\n",
    "            last *= 5\n",
    "            if len(str(last)) > 30:\n",
    "                first = int(str(last)[:25])\n",
    "                flag = True\n",
    "            elif first > 1:\n",
    "                first = int(str(first * 2)[:25])\n",
    "            if flag:\n",
    "                last = int(str(last)[-5:])\n",
    "        if first > 1:\n",
    "            return str(first)[:5] + '...' + (5 - len(str(last))) * '0' + str(last) + 'e' + str(count_zero)\n",
    "        if len(str(last)) > 10:\n",
    "            return str(last)[:5] + '...' + str(last)[-5:] + 'e' + str(count_zero)\n",
    "        return str(last) + 'e' + str(count_zero)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def abbreviateProduct(self, left: int, right: int) -> str:\n",
    "        suffix = 1\n",
    "        cnt0 = 0\n",
    "        mod = 10 ** 20 \n",
    "        for i in range(left, right + 1):\n",
    "            suffix *= i \n",
    "            while suffix and suffix % 10 == 0:\n",
    "                suffix //= 10 \n",
    "                cnt0 += 1 \n",
    "            suffix %= 10 ** 20 \n",
    "        prefix = 1\n",
    "        flag = False \n",
    "        for i in range(left, right + 1):\n",
    "            prefix *= i \n",
    "            while prefix > 10 and prefix % 10 == 0:\n",
    "                prefix //= 10 \n",
    "            while prefix > mod:\n",
    "                prefix //= 10 \n",
    "        if prefix >= 10 ** 10:\n",
    "            flag = True\n",
    "        \n",
    "        old = prefix\n",
    "        while prefix >= 10 ** 5:\n",
    "            prefix //= 10 \n",
    "        if flag:\n",
    "            return str(prefix) + '...' + str(suffix)[-5 : ] + 'e' + str(cnt0)\n",
    "        return str(old) + 'e' + str(cnt0)\n",
    "\n",
    "\n",
    "          "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def abbreviateProduct(self, left: int, right: int) -> str:\n",
    "        sys.set_int_max_str_digits(10**9)\n",
    "        p = 1\n",
    "        for i in range(left, right + 1):\n",
    "            p *= i\n",
    "        # 求 p 有多少个后缀 0\n",
    "        cnt = 0\n",
    "        while p % 10 == 0:\n",
    "            cnt += 1\n",
    "            p //= 10\n",
    "        # 求 p 的前 5 位\n",
    "        s = str(p)\n",
    "        d = len(s)\n",
    "        if d > 10:\n",
    "            pre = s[:5]\n",
    "            suf = s[-5:]\n",
    "            return pre + '...' + suf + 'e' + str(cnt)\n",
    "        else:\n",
    "            return s + 'e' + str(cnt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "m10 = 10000000000\n",
    "class Solution:\n",
    "    def abbreviateProduct(self, left: int, right: int) -> str:\n",
    "        c2, c5 = 0, 0\n",
    "        pre, mul, suf = 1, 1, 1\n",
    "        for x in range(left, right+1):\n",
    "            pre = int(str(pre * x)[:20])\n",
    "            while x % 2 == 0:\n",
    "                x /= 2\n",
    "                c2 += 1\n",
    "            while x % 5 == 0:\n",
    "                x /= 5\n",
    "                c5 += 1\n",
    "            suf = suf * x % 100000\n",
    "            if mul < m10:\n",
    "                mul *= x\n",
    "        c = min(c2, c5)\n",
    "        for _ in range(c2-c):\n",
    "            if mul < m10:\n",
    "                mul *= 2\n",
    "            suf = suf * 2 % 100000\n",
    "        for _ in range(c5-c):\n",
    "            if mul < m10:\n",
    "                mul *= 5\n",
    "            suf = suf * 5 % 100000\n",
    "        if mul < m10:\n",
    "            return str(int(mul)) + \"e\" + str(c)\n",
    "        else:\n",
    "            ssuf = str(int(suf))\n",
    "            while len(ssuf) < 5:\n",
    "                ssuf = \"0\" + ssuf\n",
    "            return str(int(pre))[:5] + \"...\" + ssuf + \"e\" + str(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "T1, T2 = int(10 ** 20), int(10 ** 20)\n",
    "class Solution:\n",
    "    def abbreviateProduct(self, left: int, right: int) -> str:\n",
    "        print(T1)\n",
    "        num1, num2 = left, left\n",
    "        flag = False\n",
    "        for i in range(left + 1, right + 1):\n",
    "            num1, num2 = num1 * i, num2 * i\n",
    "            if num1 > T1 and num1 % 10 != 0:\n",
    "                flag = True\n",
    "            while num1 > T1:\n",
    "                num1 //= 10\n",
    "            while num2 % 10 == 0:\n",
    "                num2 //= 10\n",
    "            num2 = int(num2) % T2\n",
    "        zero = \"e\" + str(self.count_zero(left, right))\n",
    "        if not flag:\n",
    "            while num1 % 10 == 0:\n",
    "                num1 //= 10\n",
    "        if num1 < 10 ** 10:\n",
    "            return str(num1) + zero\n",
    "        while num1 >= 10 ** 5:\n",
    "            num1 //= 10\n",
    "        temp = str(num2)\n",
    "        return str(num1) + \"...\" + temp[len(temp) - 5:] + zero\n",
    "\n",
    "\n",
    "    def count_zero(self, left, right):\n",
    "        two, five = 0, 0\n",
    "\n",
    "        def f(num):\n",
    "            a, b = 0, 0\n",
    "            while num % 2 == 0:\n",
    "                num, a = num / 2, a + 1\n",
    "            while num % 5 == 0:\n",
    "                num, b = num / 5, b + 1\n",
    "            return a, b\n",
    "\n",
    "        for i in range(left, right + 1):\n",
    "            temp = f(i)\n",
    "            two, five = two + temp[0], five + temp[1]\n",
    "        return min(two, five)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def abbreviateProduct(self, left: int, right: int) -> str:\n",
    "        C, maxpre, mod, maxval = 0, 10 ** 24, 10 ** 10, 10 ** 12\n",
    "        val, pre, suf = 1,1,1\n",
    "        for i in range(left, right + 1):\n",
    "            pre *= i\n",
    "            suf *= i\n",
    "            last = 0\n",
    "            while pre > maxpre :\n",
    "                last = pre % 10\n",
    "                pre = pre // 10\n",
    "            if last >= 5:\n",
    "                pre += 1\n",
    "            while suf % 10 == 0:\n",
    "                suf //= 10\n",
    "                C += 1\n",
    "            suf %= mod\n",
    "            if val <= maxval:\n",
    "                val *= i\n",
    "                while val % 10 == 0:\n",
    "                    val //= 10\n",
    "        \n",
    "        if len(str(val)) <= 10:\n",
    "            return str(val) + 'e' + str(C)\n",
    "        else:\n",
    "            p, s = str(pre), str(suf)\n",
    "            while len(p) > 5:\n",
    "                p = p[:-1]\n",
    "            while len(s) > 5:\n",
    "                s = s[1:]\n",
    "            while len(s) < 5:\n",
    "                s = '0' + s\n",
    "            return p + '...' + s + 'e' + str(C)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def abbreviateProduct(self, left: int, right: int) -> str:\n",
    "        cnt2 = cnt5 = 0\n",
    "        for x in range(left, right + 1):\n",
    "            while x % 2 == 0:\n",
    "                cnt2 += 1\n",
    "                x //= 2\n",
    "            while x % 5 == 0:\n",
    "                cnt5 += 1\n",
    "                x //= 5\n",
    "        c = cnt2 = cnt5 = min(cnt2, cnt5)\n",
    "        pre = suf = 1\n",
    "        gt = False\n",
    "        for x in range(left, right + 1):\n",
    "            suf *= x\n",
    "            while cnt2 and suf % 2 == 0:\n",
    "                suf //= 2\n",
    "                cnt2 -= 1\n",
    "            while cnt5 and suf % 5 == 0:\n",
    "                suf //= 5\n",
    "                cnt5 -= 1\n",
    "            if suf >= 1e10:\n",
    "                gt = True\n",
    "                suf %= int(1e10)\n",
    "            pre *= x\n",
    "            while pre > 1e5:\n",
    "                pre /= 10\n",
    "        if gt:\n",
    "            return str(int(pre)) + \"...\" + str(suf % int(1e5)).zfill(5) + 'e' + str(c)\n",
    "        return str(suf) + \"e\" + str(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def abbreviateProduct(self, left: int, right: int) -> str:\n",
    "        suffix = 1\n",
    "        cnt0 = 0\n",
    "        mod = 10 ** 20 \n",
    "        for i in range(left, right + 1):\n",
    "            suffix *= i \n",
    "            while suffix and suffix % 10 == 0:\n",
    "                suffix //= 10 \n",
    "                cnt0 += 1 \n",
    "            suffix %= 10 ** 20 \n",
    "        prefix = 1\n",
    "        flag = False \n",
    "        for i in range(left, right + 1):\n",
    "            prefix *= i \n",
    "            while prefix > 10 and prefix % 10 == 0:\n",
    "                prefix //= 10 \n",
    "            while prefix > mod:\n",
    "                prefix //= 10 \n",
    "        if prefix >= 10 ** 10:\n",
    "            flag = True\n",
    "        \n",
    "        old = prefix\n",
    "        while prefix >= 10 ** 5:\n",
    "            prefix //= 10 \n",
    "        if flag:\n",
    "            return str(prefix) + '...' + str(suffix)[-5 : ] + 'e' + str(cnt0)\n",
    "        return str(old) + 'e' + str(cnt0)\n",
    "\n",
    "\n",
    "          "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "sys.set_int_max_str_digits(0)  # 大数的范围坑\n",
    "\n",
    "\n",
    "def ac_max(a, b):\n",
    "    return a if a > b else b\n",
    "\n",
    "\n",
    "def ac_min(a, b):\n",
    "    return a if a < b else b\n",
    "\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def abbreviateProduct(self, left: int, right: int) -> str:\n",
    "        ans = 1\n",
    "        cnt = 0\n",
    "        for num in range(left, right+1):\n",
    "            ans *= num\n",
    "\n",
    "            while ans % 10 == 0:\n",
    "                ans //= 10\n",
    "                cnt += 1\n",
    "        \n",
    "        s = str(ans)\n",
    "        if len(s) > 10:\n",
    "            s = s[:5] + \"...\" + s[-5:]\n",
    "        return s+\"e\"+str(cnt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import sys\n",
    "sys.set_int_max_str_digits(0)\n",
    "class Solution:\n",
    "    def abbreviateProduct(self, left: int, right: int) -> str:\n",
    "        count = 0\n",
    "        result = 1\n",
    "        for i in range(left,right+1):\n",
    "            result *= i\n",
    "            while result % 10 == 0:\n",
    "                count += 1\n",
    "                result //= 10\n",
    "        result = str(result)\n",
    "        if len(result) > 10:\n",
    "            return result[:5] + \"...\" + result[-5:] + \"e\" + str(count)\n",
    "        else:\n",
    "            return result + \"e\" + str(count)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def abbreviateProduct(self, left, right):\n",
    "        \"\"\"\n",
    "        :type left: int\n",
    "        :type right: int\n",
    "        :rtype: str\n",
    "        \"\"\"\n",
    "        import sys\n",
    "        p=1\n",
    "        for i in range(left,right+1):\n",
    "           p=p*i\n",
    "        sys.set_int_max_str_digits(400000)\n",
    "        q=str(p)\n",
    "        l=0\n",
    "        for k in q[::-1]:\n",
    "           if k!='0':\n",
    "              break\n",
    "           else:\n",
    "              l=l+1\n",
    "        if l==0:\n",
    "           return f\"{q}e0\" \n",
    "        t=q[:-l]\n",
    "        if len(t)>10:\n",
    "            return f'{t[:5]}...{t[-5:]}e{str(l)}'\n",
    "        return  f'{t}e{str(l)}'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def abbreviateProduct(self, left: int, right: int) -> str:\n",
    "        suffix = 1\n",
    "        cnt0 = 0\n",
    "        mod = 10 ** 20 \n",
    "        for i in range(left, right + 1):\n",
    "            suffix *= i \n",
    "            while suffix and suffix % 10 == 0:\n",
    "                suffix //= 10 \n",
    "                cnt0 += 1 \n",
    "            suffix %= 10 ** 20 \n",
    "        prefix = 1\n",
    "        flag = False \n",
    "        for i in range(left, right + 1):\n",
    "            prefix *= i \n",
    "            while prefix > 10 and prefix % 10 == 0:\n",
    "                prefix //= 10 \n",
    "            while prefix > mod:\n",
    "                prefix //= 10 \n",
    "        if prefix >= 10 ** 10:\n",
    "            flag = True\n",
    "        \n",
    "        old = prefix\n",
    "        while prefix >= 10 ** 5:\n",
    "            prefix //= 10 \n",
    "        if flag:\n",
    "            return str(prefix) + '...' + str(suffix)[-5 : ] + 'e' + str(cnt0)\n",
    "        return str(old) + 'e' + str(cnt0)\n",
    "\n",
    "\n",
    "          "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def abbreviateProduct(self, left, right):\n",
    "        \"\"\"\n",
    "        :type left: int\n",
    "        :type right: int\n",
    "        :rtype: str\n",
    "        \"\"\"\n",
    "        import sys\n",
    "        p=1\n",
    "        for i in range(left,right+1):\n",
    "           p=p*i\n",
    "        sys.set_int_max_str_digits(400000)\n",
    "        q=str(p)\n",
    "        l=0\n",
    "        for k in q[::-1]:\n",
    "           if k!='0':\n",
    "              break\n",
    "           else:\n",
    "              l=l+1\n",
    "        if l==0:\n",
    "           return f\"{q}e0\" \n",
    "        t=q[:-l]\n",
    "        if len(t)>10:\n",
    "            return f'{t[:5]}...{t[-5:]}e{str(l)}'\n",
    "        return  f'{t}e{str(l)}'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def abbreviateProduct(self, left: int, right: int) -> str:\n",
    "        import sys\n",
    "        p=1\n",
    "        for i in range(left,right+1):\n",
    "           p=p*i\n",
    "        sys.set_int_max_str_digits(0)\n",
    "        q=str(p)\n",
    "        l=0\n",
    "        for k in q[::-1]:\n",
    "           if k!='0':\n",
    "              break\n",
    "           else:\n",
    "              l=l+1\n",
    "        if l==0:\n",
    "           return f'{q}e0'\n",
    "        t=q[:-l]\n",
    "        if len(t)>10:\n",
    "            return f'{t[:5]}...{t[-5:]}e{str(l)}'\n",
    "        return  f'{t}e{str(l)}'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import sys\n",
    "\n",
    "sys.set_int_max_str_digits(0)\n",
    "class Solution:\n",
    "    def abbreviateProduct(self, left: int, right: int) -> str:\n",
    "\n",
    "        product = 1\n",
    "        for a in range(left, right+1):\n",
    "            product *= a\n",
    "        product = str(product)\n",
    "        n = len(product)\n",
    "        i = n-1\n",
    "        while product[i] == '0':\n",
    "            i-=1\n",
    "        trailzero = n-i-1\n",
    "        ans = 'e'+ str(trailzero)\n",
    "        leading = n - trailzero\n",
    "        if leading <= 10:\n",
    "            strleading = product[:leading]\n",
    "        else:\n",
    "            strleading = product[:5] + '...' + product[leading-5:leading]\n",
    "        return strleading+ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "T1, T2 = 10 ** 20, 10 ** 11\n",
    "class Solution:\n",
    "    def abbreviateProduct(self, left: int, right: int) -> str:\n",
    "        num1, num2 = left, left\n",
    "        flag = False\n",
    "        for i in range(left + 1, right + 1):\n",
    "            num1, num2 = num1 * i, num2 * i\n",
    "            if num1 > T1 and num1 % 10 != 0:\n",
    "                flag = True\n",
    "            while num1 > T1:\n",
    "                num1 /= 10\n",
    "            while num2 % 10 == 0:\n",
    "                num2 /= 10\n",
    "            num2 %= T2\n",
    "        zero = \"e\" + str(self.count_zero(left, right))\n",
    "        if not flag:\n",
    "            while num1 % 10 == 0:\n",
    "                num1 /= 10\n",
    "        num1 = int(num1)\n",
    "        if num1 < 10 ** 10:\n",
    "            return str(num1) + zero\n",
    "        while num1 > 10 ** 5:\n",
    "            num1 /= 10\n",
    "        num1, num2 = int(num1), int(num2)\n",
    "        temp = str(num2)\n",
    "        print(temp)\n",
    "        return str(num1) + \"...\" + temp[len(temp) - 5:] + zero\n",
    "\n",
    "\n",
    "    def count_zero(self, left, right):\n",
    "        two, five = 0, 0\n",
    "\n",
    "        def f(num):\n",
    "            a, b = 0, 0\n",
    "            while num % 2 == 0:\n",
    "                num, a = num / 2, a + 1\n",
    "            while num % 5 == 0:\n",
    "                num, b = num / 5, b + 1\n",
    "            return a, b\n",
    "\n",
    "        for i in range(left, right + 1):\n",
    "            temp = f(i)\n",
    "            two, five = two + temp[0], five + temp[1]\n",
    "        return min(two, five)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import sys\n",
    "sys.set_int_max_str_digits(99999)\n",
    "class Solution:\n",
    "    def abbreviateProduct(self, left: int, right: int) -> str:\n",
    "        \"\"\"\n",
    "\n",
    "        :param left:\n",
    "        :param right:\n",
    "        :return:\n",
    "        \"\"\"\n",
    "        ret = 1\n",
    "        for i in range(left, right+1):\n",
    "            ret *= i\n",
    "        ll = str(ret)\n",
    "        res2 = self.changeW(ll)\n",
    "        return res2\n",
    "\n",
    "    def changeW(self, s):\n",
    "        cnt = 0\n",
    "        cur = -1\n",
    "        for i in range(len(s)):\n",
    "            pt = len(s) - 1 -i\n",
    "            if s[pt] == '0':\n",
    "                cnt += 1\n",
    "            else:\n",
    "                cur = pt\n",
    "                break\n",
    "        ret1 = self.cutW(s[:cur+1]) + 'e' + str(cnt)\n",
    "        return ret1\n",
    "\n",
    "    def cutW(self, s):\n",
    "        if len(s) <= 10:\n",
    "            return s\n",
    "        return s[:5] + '...'+ s[len(s)-5:]\n",
    "\n",
    "\n",
    "a = Solution()\n",
    "print(a.abbreviateProduct(left = 44, right = 9556))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def abbreviateProduct(self, left: int, right: int) -> str:\n",
    "        ans=1\n",
    "        tot=0\n",
    "        for i in range(left,right+1):\n",
    "            ans*=i\n",
    "        while ans%10==0:\n",
    "            tot+=1\n",
    "            ans=ans//10\n",
    "        digits= int(math.log10(ans))+1\n",
    "        ans1 = str(ans //(10**(digits-5)))\n",
    "        ans2 = str(ans % 100000)\n",
    "        while len(ans2)<5:\n",
    "            ans2='0'+ans2\n",
    "        if digits>10:\n",
    "            s=ans1+'...'+ans2\n",
    "        else:\n",
    "            s = str(ans)\n",
    "        s+='e'+str(tot)\n",
    "        return s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def abbreviateProduct(self, left: int, right: int) -> str:\n",
    "\n",
    "        sys.set_int_max_str_digits(10**5)\n",
    "        num = left\n",
    "        for j in range(1+left,1+right):\n",
    "            num*=j\n",
    "        st = str(num)\n",
    "\n",
    "        p = len(st)-1\n",
    "        cnt = 0\n",
    "        while True:\n",
    "            if st[p]!='0':\n",
    "                break\n",
    "            else:\n",
    "                cnt+=1\n",
    "                p-=1\n",
    "        st = st[:-cnt] if cnt else st\n",
    "        s1 = st if len(st)<=10 else st[:5]+'...'+st[-5:]\n",
    "        return s1+'e'+str(cnt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def abbreviateProduct(self, left: int, right: int) -> str:\n",
    "        a = [0, 1]\n",
    "        M = 10 ** 10\n",
    "        for i in range(left, right + 1):\n",
    "            a[1] *= i\n",
    "        while a[1] % 10 == 0:\n",
    "            a[0] += 1\n",
    "            a[1] = a[1] // 10\n",
    "        if a[1] <= M:\n",
    "            return str(a[1])+ 'e' + str(a[0])\n",
    "\n",
    "        suf=a[1]%(M*1000)\n",
    "        if len(str(left))==1:\n",
    "            n=10**(len(str(left))*(right-left)-10)\n",
    "        else:\n",
    "            n=10**((len(str(left))-1)*(right-left)-10)\n",
    "        pre=a[1]//n\n",
    "\n",
    "        while pre > M:\n",
    "            pre = pre//100000\n",
    "        else:\n",
    "            return  str(pre)[:5]+ '...' + str(suf)[-5:] + 'e' + str(a[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def abbreviateProduct(self, left, right):\n",
    "        lst, cnt10, cnt5, ans = list(range(left, right + 1)), 0, 0, 1\n",
    "        for i in range(right - left + 1):\n",
    "            while lst[i] % 10 == 0:\n",
    "                cnt10 += 1\n",
    "                lst[i] //= 10\n",
    "            while lst[i] % 5 == 0:\n",
    "                cnt5 += 1\n",
    "                lst[i] //= 5\n",
    "        for i in range(right - left + 1):\n",
    "            while cnt5 and lst[i] % 2 == 0:\n",
    "                lst[i] >>= 1\n",
    "                cnt5 -= 1\n",
    "                cnt10 += 1\n",
    "            if type(ans) == int:\n",
    "                ans *= lst[i]\n",
    "                if ans >= 10000000000: ans = ans, ans % 100000\n",
    "            else:\n",
    "                ans = int(str(ans[0] * lst[i])[:20]), ans[1] * lst[i] %  100000\n",
    "        if cnt5:\n",
    "            tmp = 5 ** cnt5\n",
    "            if type(ans) == int:\n",
    "                ans *= tmp\n",
    "                if ans >= 10000000000: ans = ans, ans % 100000\n",
    "            else:\n",
    "                ans = ans[0] * tmp, ans[1] * tmp %  100000\n",
    "        return (str(ans) if type(ans) == int else (str(ans[0])[:5] + \"...\" + str(ans[1]).zfill(5))) + 'e' + str(cnt10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def abbreviateProduct(self, left, right):\n",
    "        lst, cnt10, cnt5, ans = list(range(left, right + 1)), 0, 0, 1\n",
    "        for i in range(right - left + 1):\n",
    "            while lst[i] % 10 == 0:\n",
    "                cnt10 += 1\n",
    "                lst[i] //= 10\n",
    "            while lst[i] % 5 == 0:\n",
    "                cnt5 += 1\n",
    "                lst[i] //= 5\n",
    "        for i in range(right - left + 1):\n",
    "            while cnt5 and lst[i] % 2 == 0:\n",
    "                lst[i] >>= 1\n",
    "                cnt5 -= 1\n",
    "                cnt10 += 1\n",
    "            if type(ans) == int:\n",
    "                ans *= lst[i]\n",
    "                if ans >= 10000000000: ans = ans, ans % 100000\n",
    "            else:\n",
    "                ans = int(str(ans[0] * lst[i])[:20]), ans[1] * lst[i] %  100000\n",
    "        if cnt5:\n",
    "            tmp = 5 ** cnt5\n",
    "            if type(ans) == int:\n",
    "                ans *= tmp\n",
    "                if ans >= 10000000000: ans = ans, ans % 100000\n",
    "            else:\n",
    "                ans = ans[0] * tmp, ans[1] * tmp %  100000\n",
    "        return (str(ans) if type(ans) == int else (str(ans[0])[:5] + \"...\" + str(ans[1]).zfill(5))) + 'e' + str(cnt10)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def abbreviateProduct(self, left, right):\n",
    "        lst, cnt10, cnt5, ans = list(range(left, right + 1)), 0, 0, 1\n",
    "        for i in range(right - left + 1):\n",
    "            while lst[i] % 10 == 0:\n",
    "                cnt10 += 1\n",
    "                lst[i] //= 10\n",
    "            while lst[i] % 5 == 0:\n",
    "                cnt5 += 1\n",
    "                lst[i] //= 5\n",
    "        for i in range(right - left + 1):\n",
    "            while cnt5 and lst[i] % 2 == 0:\n",
    "                lst[i] >>= 1\n",
    "                cnt5 -= 1\n",
    "                cnt10 += 1\n",
    "            if type(ans) == int:\n",
    "                ans *= lst[i]\n",
    "                if ans >= 10000000000: ans = ans, ans % 100000\n",
    "            else:\n",
    "                ans = int(str(ans[0] * lst[i])[:20]), ans[1] * lst[i] %  100000\n",
    "        if cnt5:\n",
    "            tmp = 5 ** cnt5\n",
    "            if type(ans) == int:\n",
    "                ans *= tmp\n",
    "                if ans >= 10000000000: ans = ans, ans % 100000\n",
    "            else:\n",
    "                ans = ans[0] * tmp, ans[1] * tmp %  100000\n",
    "        return (str(ans) if type(ans) == int else (str(ans[0])[:5] + \"...\" + str(ans[1]).zfill(5))) + 'e' + str(cnt10)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def abbreviateProduct(self, left, right):\n",
    "        # 初始化整数列表，以及10和5的计数器\n",
    "        lst, cnt10, cnt5, ans = list(range(left, right + 1)), 0, 0, 1\n",
    "\n",
    "        # 计算后缀0的数量\n",
    "        for i in range(right - left + 1):\n",
    "            while lst[i] % 10 == 0:\n",
    "                cnt10 += 1\n",
    "                lst[i] //= 10\n",
    "            while lst[i] % 5 == 0:\n",
    "                cnt5 += 1\n",
    "                lst[i] //= 5\n",
    "        \n",
    "        # 尝试减少5的数量，通过将其与2匹配\n",
    "        for i in range(right - left + 1):\n",
    "            while cnt5 and lst[i] % 2 == 0:\n",
    "                lst[i] >>= 1\n",
    "                cnt5 -= 1\n",
    "                cnt10 += 1\n",
    "\n",
    "            # 计算乘积，并确保乘积不会过大\n",
    "            if type(ans) == int:\n",
    "                ans *= lst[i]\n",
    "                if ans >= 10000000000:\n",
    "                    ans = ans, ans % 100000\n",
    "            else:\n",
    "                ans = int(str(ans[0] * lst[i])[:20]), ans[1] * lst[i] %  100000\n",
    "        \n",
    "        # 如果仍然有乘数5，则将其乘以乘积\n",
    "        if cnt5:\n",
    "            tmp = 5 ** cnt5\n",
    "            if type(ans) == int:\n",
    "                ans *= tmp\n",
    "                if ans >= 10000000000: \n",
    "                    ans = ans, ans % 100000\n",
    "            else:\n",
    "                ans = ans[0] * tmp, ans[1] * tmp %  100000\n",
    "\n",
    "        # 格式化结果\n",
    "        return (str(ans) if type(ans) == int else (str(ans[0])[:5] + \"...\" + str(ans[1]).zfill(5))) + 'e' + str(cnt10)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "A = [2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192]\n",
    "B = [5, 25, 125, 625, 3125]\n",
    "sys.set_int_max_str_digits(10**5)\n",
    "class Solution:\n",
    "    def abbreviateProduct(self, left: int, right: int) -> str:\n",
    "        c2,c5 = 0,0\n",
    "        for i in range(left,right+1):\n",
    "            p2,p5 = 0,0\n",
    "            while p2 < 13 and not i % A[p2]:\n",
    "                p2 += 1\n",
    "            while p5 < 5 and not i % B[p5]:\n",
    "                p5 += 1\n",
    "            c2 += p2;c5 += p5\n",
    "        C = min(c2,c5)\n",
    "        L = []\n",
    "        c2,c5 = C,C\n",
    "        for i in range(left,right+1):\n",
    "            v = i\n",
    "            while c2 > 0 and not v % 2:\n",
    "                v //= 2;c2 -= 1\n",
    "            while c5 > 0 and not v % 5:\n",
    "                v //= 5;c5 -= 1\n",
    "            L.append(v)\n",
    "        res = 1;f = 1\n",
    "        for i in L:\n",
    "            res *= i\n",
    "            if res >= 10**10:\n",
    "                f = 0;break\n",
    "        if f:return str(res)+'e'+str(C)\n",
    "        pre,suf = 1,1\n",
    "        for i in L:\n",
    "            pre *= i\n",
    "            suf *= i\n",
    "            if suf >= 10**6:\n",
    "                ssuf = str(suf)[-5:]\n",
    "                suf = int(ssuf)\n",
    "        sssuf = str(suf)[-5:]\n",
    "        if len(sssuf) < 5:\n",
    "            sssuf = '0'*(5-len(sssuf)) + sssuf\n",
    "        return str(pre)[:5]+'...'+sssuf+'e'+str(C)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import numpy\n",
    "\n",
    "class Solution:\n",
    "    def abbreviateProduct(self, left: int, right: int) -> str:\n",
    "        cnt2 = cnt5 = 0\n",
    "        z = numpy.float128(0)\n",
    "        for x in range(left, right + 1):\n",
    "            z += numpy.log10(x)\n",
    "            while x % 2 == 0:\n",
    "                x //= 2\n",
    "                cnt2 += 1\n",
    "            while x % 5 == 0:\n",
    "                x //= 5\n",
    "                cnt5 += 1\n",
    "        c = cnt2 = cnt5 = min(cnt2, cnt5)\n",
    "        suf = y = 1\n",
    "        gt = False\n",
    "        for x in range(left, right + 1):\n",
    "            while cnt2 and x % 2 == 0:\n",
    "                x //= 2\n",
    "                cnt2 -= 1\n",
    "            while cnt5 and x % 5 == 0:\n",
    "                x //= 5\n",
    "                cnt5 -= 1\n",
    "            suf = suf * x % 100000\n",
    "            if not gt:\n",
    "                y *= x\n",
    "                gt = y >= 1e10\n",
    "        if not gt:\n",
    "            return str(y) + 'e' + str(c)\n",
    "        pre = int(pow(10, z - int(z) + 4))\n",
    "        return str(pre) + '...' + str(suf).zfill(5) + 'e' + str(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import numpy\n",
    "\n",
    "class Solution:\n",
    "    def abbreviateProduct(self, left: int, right: int) -> str:\n",
    "        log_prod = numpy.float128(0)\n",
    "        last = 1\n",
    "        num_zeros = 0\n",
    "        for x in range(left, right+1):\n",
    "            log_prod += numpy.log10(x)\n",
    "            last *= x\n",
    "            while last % 10 == 0:\n",
    "                last //= 10\n",
    "                num_zeros += 1\n",
    "            last %= 1000000000000\n",
    "        x = floor(log_prod)\n",
    "        if x - num_zeros < 10:\n",
    "            return \"%de%d\" % (last % 10000000000, num_zeros)\n",
    "        return \"%.0f...%05de%d\" % (floor(numpy.power(10, (4 + log_prod - x))), last % 100000, num_zeros)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import numpy\n",
    "\n",
    "class Solution:\n",
    "    def abbreviateProduct(self, left: int, right: int) -> str:\n",
    "        cnt2 = cnt5 = 0\n",
    "        z = numpy.float128(0)\n",
    "        for x in range(left, right + 1):\n",
    "            z += numpy.log10(x)\n",
    "            while x and x % 2 == 0:\n",
    "                x //= 2\n",
    "                cnt2 += 1\n",
    "            while x and x % 5 == 0:\n",
    "                x //= 5\n",
    "                cnt5 += 1\n",
    "        c = cnt2 = cnt5 = min(cnt2, cnt5)\n",
    "        suf = y = 1\n",
    "        gt = False\n",
    "        for x in range(left, right + 1):\n",
    "            while x and cnt2 and x % 2 == 0:\n",
    "                x //= 2\n",
    "                cnt2 -= 1\n",
    "            while x and cnt5 and x % 5 == 0:\n",
    "                x //= 5\n",
    "                cnt5 -= 1\n",
    "            suf = suf * x % 100000\n",
    "            if not gt:\n",
    "                y *= x\n",
    "                gt = y >= 1e10\n",
    "        if not gt:\n",
    "            return str(y) + 'e' + str(c)\n",
    "        pre = str(pow(10, z - int(z) + 4))[:5]\n",
    "        return pre + '...' + str(suf).zfill(5) + 'e' + str(c)"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
